package com.englisheveryday.activity;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.englisheveryday.R;
import com.englisheveryday.constants.Constants;
import com.englisheveryday.media.RichMediaPlayer;
import com.englisheveryday.net.Downloader;
import com.englisheveryday.net.Downloader.OnCompleteListener;
import com.englisheveryday.service.ArticleService;
import com.englisheveryday.service.PlaylistService;
import com.englisheveryday.utils.L;
import com.englisheveryday.utils.LocaleUtil;
import com.englisheveryday.utils.StringUtil;
import com.englisheveryday.vo.ArticleVO;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-10-8 下午3:01:35
 * @version 1.1
 */

class ArticleListViewAdapter extends BaseAdapter {

	private Context context;
	
	private List<ArticleVO> list;
	
	private RichMediaPlayer player;
	
	private int clickCount;
	
	private long firstClickTime;
	
	private long secondClickTime;
	
	private ArticleService articleService;
	
	private PlaylistService playlistService;
	   
	public ArticleListViewAdapter(Context context,List<ArticleVO> list,RichMediaPlayer player){
		this.context = context;
		this.list = list;
		this.player = player;
		this.articleService = new ArticleService(context);
		this.playlistService = new PlaylistService(context);
	}
	   
	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View contentView,final ViewGroup viewGroup) {
		final ArticleVO vo = list.get(position);
		if(contentView==null)
		contentView = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.article_list_item, null);
		//设置组建
		EnglishEveryDayStaticVO.title = (TextView)contentView.findViewById(R.id.title);
		EnglishEveryDayStaticVO.englishtext = (TextView)contentView.findViewById(R.id.englishtext);
		EnglishEveryDayStaticVO.category = (TextView)contentView.findViewById(R.id.category);
		EnglishEveryDayStaticVO.createtime = (TextView)contentView.findViewById(R.id.createtime);
		final ImageView markImage = (ImageView)contentView.findViewById(R.id.mark);
		final ImageView toplaylistView = (ImageView)contentView.findViewById(R.id.toplaylist);
		RelativeLayout moreBarButtonLayout = (RelativeLayout)contentView.findViewById(R.id.moreBarButtonLayout);
		final RelativeLayout itemOptLayout = (RelativeLayout)contentView.findViewById(R.id.moreBarLayout);
		//设置组建值
		EnglishEveryDayStaticVO.title.setText(StringUtil.ToDBC(vo.getTitle()));
		EnglishEveryDayStaticVO.englishtext.setText(StringUtil.ToDBC(vo.getEnglishText()));
		EnglishEveryDayStaticVO.createtime.setText(LocaleUtil.toLocaleDate(vo.getCreatetime()));
		EnglishEveryDayStaticVO.category.setText(StringUtil.ToDBC(vo.getCategory()));
		//设置是否收藏
		L.d(vo.getCategory());
		setMarkState(markImage,articleService.isMark(vo.getId()));
		//获得播放地址
		final String path = vo.getAudio();
		moreBarButtonLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int count  = viewGroup.getChildCount();
				for(int i=0;i<count;i++){
					View view = viewGroup.getChildAt(i);
					View view2 = view.findViewById(R.id.moreBarLayout);
					if(view2!=null)
					view2.setVisibility(View.GONE);
				}
				L.d("count"+count);
				if(itemOptLayout.getVisibility() == View.VISIBLE)
					itemOptLayout.setVisibility(View.GONE);
				else 
					itemOptLayout.setVisibility(View.VISIBLE);
			}
		});
		
		markImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(articleService.isMark(vo.getId())){
					articleService.unMark(vo.getId());
					Toast.makeText(context, R.string.unmark_success, Toast.LENGTH_SHORT).show();
					setMarkState(markImage,false);
				}else {
					articleService.mark(vo.getId());
					setMarkState(markImage,true);
					Toast.makeText(context, R.string.mark_success, Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		toplaylistView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				playlistService.addToPlayList(vo.getId());
				Toast.makeText(context, R.string.toplaylist_success, Toast.LENGTH_SHORT).show();
			}
		});
		
		contentView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(v.getId()==R.id.englishtext){
				if(clickCount==0){
					firstClickTime = System.currentTimeMillis();
					clickCount++;
				}else if(clickCount==1){
					secondClickTime = System.currentTimeMillis();
					//如果两次点击时间小于500
					if((secondClickTime-firstClickTime)<500){
						//判断文件是否存在
						File file = new File(Constants.BASEDIR+path);
						if(file.exists()){
							player.setPath(Constants.BASEDIR+path);
							player.play();
						}else {
							Downloader downloader = new Downloader();
							downloader.downloadFromUrl(Constants.URL+path, Constants.BASEDIR+path);
							downloader.setOnCompleteListener(new OnCompleteListener() {
								@Override
								public void onComplete(String fileName) {
									player.setPath(Constants.BASEDIR+fileName);
									player.play();
								}
							});
						}
						clickCount = 0;
					}else {
						firstClickTime = secondClickTime;
					}
				}
			}
			}
		});
		

		return contentView;
	}
	//listView 静态类，用于减少内存开销
	static class EnglishEveryDayStaticVO {
		   private static TextView title;
		   private static TextView englishtext;
		   private static TextView category;
		   private static TextView createtime;
		   
	}
	
	private void setMarkState(ImageView imageView,boolean state){
		if(state){
			imageView.setImageResource(R.drawable.list_item_loved);
		}else {
			imageView.setImageResource(R.drawable.list_item_love_icon_normal);
		}
	}
  }

